package com.ustadzmenjawab.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import com.ustadzmenjawab.indexing.LuceneIndexing;

public class TransferIndex {

	/**
	 * @param args
	 */
	public static final String FTP_SERVER = "ustadzmenjawab.com";
	private static final String INDEX_REMOTE_LOC = "/webapps/UstadzMenjawab/WEB-INF/index";
	private static final String username = "the_great_nelman";
	private static final String password = "kuntum";
	
	public static void main(String[] args){
		TransferIndex ti = new TransferIndex();
		ti.storeIndex(LuceneIndexing.INDEX_LOC, INDEX_REMOTE_LOC);
	}
	
	public TransferIndex(){
		
	}
	
	public boolean storeIndex(String indexPathSrc, String indexPathDest){
		boolean error = false;
		 FTPClient ftp = new FTPClient();
	    try {
	      int reply;
	      System.out.println("Connecting to" + FTP_SERVER);
	      ftp.connect(FTP_SERVER);
	      ftp.login(username, password);
	      System.out.println("Connected to " + FTP_SERVER + ".");
	      System.out.print("status: " + ftp.getReplyString());

	      // After connection attempt, you should check the reply code to verify
	      // success.
	      reply = ftp.getReplyCode();

	      if(!FTPReply.isPositiveCompletion(reply)) {
	        ftp.disconnect();
	        System.err.println("FTP server refused connection.");
	        return false;
	      }
	      File indexFold = new File(indexPathSrc);
	      File[] indexFiles = indexFold.listFiles();
	      ftp.setControlKeepAliveTimeout(120);
	      FTPFile[] svrfiles = ftp.listFiles(indexPathDest);
	      for(FTPFile svrFile : svrfiles){
	    	  if(svrFile.getName().equals(".") || svrFile.getName().equals("..")){
	    		  continue;
	    	  }
	    	  if(!ftp.deleteFile(indexPathDest+"/"+svrFile.getName())){
	    		  System.out.println("Delete "+indexPathDest+"/"+svrFile.getName() + " unsuccesful." );
	    		  
	    	  }else{
	    		  System.out.println("Delete "+indexPathDest+"/"+svrFile.getName() + " succesful." );
	    	  }
	      }
	      ftp.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
	      ftp.setConnectTimeout(500 * 1000);
	      for(File indFile : indexFiles){
	    	  InputStream is = new FileInputStream(indFile);
	    	  ftp.setBufferSize(1 * 1024 * 512);
	    	  try{
	    		  System.out.println("Going to transfer " + indexPathDest+"/"+indFile.getName());
	    		  if(!ftp.storeFile(indexPathDest+"/"+indFile.getName(), is)){
	    			  System.out.println("Failed transfering " + indFile + " to " + indexPathDest+"/"+indFile.getName());
	    		  }else{
	    			  System.out.println("Succesful transfering " + indFile + " to " + indexPathDest+"/"+indFile.getName());
	    		  }
	    	  }catch(IOException e){
	    		  e.printStackTrace();
	    	  }finally{

		    	  is.close();
	    	  }
	      }
	      ftp.logout();
	      System.out.println("Transfer finish.");
	    } catch(IOException e) {
	      error = true;
	      e.printStackTrace();
	    } finally {
	      if(ftp.isConnected()) {
	        try {
	          ftp.disconnect();
	        } catch(IOException ioe) {
	          // do nothing
	        }
	      }
	      System.exit(error ? 1 : 0);
	    }
		
		
		return false;
	}

}
